package com.shigen.demo.atguigu;

import java.util.concurrent.CountDownLatch;
import lombok.Getter;

/**
 * CountDownLatchDemo
 *
 * @author shigenfu
 * @date 2022/5/22 6:09 下午
 */
public class CountDownLatchDemo {

  public static void main(String[] args) throws InterruptedException {
    CountDownLatch countDownLatch = new CountDownLatch(6);

    for (int i = 1; i <= 6; i++) {
      new Thread(() -> {
        System.out.println(Thread.currentThread().getName() + " 离开教室....");
        // * Decrements the count of the latch, releasing all waiting threads if
        // * the count reaches zero.
        countDownLatch.countDown();
      }, CountryEnum.getName(i)).start();
    }
    // Causes the current thread to wait until the latch has counted down to
    // * zero, unless the thread is {@linkplain Thread#interrupt interrupted}.
    countDownLatch.await();
    System.out.println(Thread.currentThread().getName() + " ********关闭");
    // closeDoor();
  }

  public static void closeDoor() throws InterruptedException {
    CountDownLatch countDownLatch = new CountDownLatch(6);

    for (int i = 0; i < 6; i++) {
      new Thread(() -> {
        System.out.println(Thread.currentThread().getName() + " 离开教室....");
        countDownLatch.countDown();
      }, String.valueOf(i)).start();
    }
    countDownLatch.await();
    System.out.println(Thread.currentThread().getName() + " ********关闭");
  }

  enum CountryEnum {
    ONE(1, "齐"), TWO(2, "楚"), THREE(3, "燕"), FOUR(4, "赵"), FIVE(5, "魏"), SIX(6, "韩");


    CountryEnum(Integer code, String msg) {
      this.code = code;
      this.msg = msg;
    }

    @Getter
    private Integer code;
    @Getter
    private String msg;

    // public Integer getCode() {
    //   return code;
    // }

    // public String getMsg() {
    //   return msg;
    // }

    public static String getName(int index) {
      CountryEnum[] values = CountryEnum.values();
      for (CountryEnum value : values) {
        if (index == value.getCode()) {
          return value.getMsg();
        }
      }
      return null;
    }
  }

}
